package com.fly.controller;

import com.fly.domain.User;
import com.fly.service.UserService;
import com.fly.vo.Response;
import com.fly.web.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Fly
 * @Description
 * @Date Created in 8:26 2018/1/31
 * @Modified by
 */
@RestController
@RequestMapping("users")
public class UserController extends BaseController {


    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    @GetMapping()
    public List<User> list() {
        return userService.findAll();
    }

    /**
     * 保存
     *
     * @param user
     * @return
     */
    @PostMapping()
    public ResponseEntity<Response> save(@RequestBody User user) {
        User rUser = userService.saveUser(user);
        return ResponseEntity.ok(new Response(true, "保存成功", rUser));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public ResponseEntity<Response> remove(@PathVariable("ids") String ids) {
        userService.removeUsers(ids);
        return ResponseEntity.ok(new Response(true, "删除成功", null));
    }
}
